package com.zy.core.util.page;


import com.zy.core.data.jpa.support.entity.page.PageBean;
import com.zy.core.data.jpa.support.entity.page.Pagination;
import com.zy.core.util.json.ComplexPropertyPreFilter;
import com.zy.core.util.json.FastJsonUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Package: core.util.page
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-10-5 22:13
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
public class LayuiPageResult extends Result {
    /**
     * 总条数
     */
    private static final String TOTAL = "count";
    /**
     * 分页集合
     */
    private static final String ROWS = "data";
    /**
     * 分页信息
     */
    private static final String PAGE = "page";

    /**
     * 状态码
     */
    private static final String CODE = "code";

    /**
     * 状态信息
     */
    private static final String MSG = "msg";

    /**
     * 默认类似谷歌分页显示数子5个
     */
    private static final Integer DEFAULT_PAGENUMBER_SIZE = 5;

    /**
     * 分页消息信息
     */
    private Map<String, Object> page;

    /**
     * 当前页码
     */
    private Integer pageNumber;
    /**
     * 当前分页大小
     */
    private Integer pageSize;
    /**
     * 总页码数
     */
    private Integer totalPage;
    /**
     * 总条数
     */
    private Long total;
    /**
     * 是否第一页
     */
    private Boolean isFirstPage;
    /**
     * 是否最后一页
     */
    private Boolean isLastPage;
    /**
     * 是否有下一页
     */
    private Boolean hasNextPage;
    /**
     * 下一页页码
     */
    private Integer nextPage;
    /**
     * 是否有上一页
     */
    private Boolean hasPrePage;
    /**
     *  上一页页码
     */
    private Integer prePage;
    /**
     * 类似于google的分页码集合
     */
    private List<PageBean.Number> numberList;

    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Boolean getIsFirstPage() {
        return isFirstPage;
    }

    public void setIsFirstPage(Boolean isFirstPage) {
        this.isFirstPage = isFirstPage;
    }

    public Boolean getIsLastPage() {
        return isLastPage;
    }

    public void setIsLastPage(Boolean isLastPage) {
        this.isLastPage = isLastPage;
    }

    public Integer getNextPage() {
        return nextPage;
    }

    public void setNextPage(Integer nextPage) {
        this.nextPage = nextPage;
    }

    public Integer getPrePage() {
        return prePage;
    }

    public void setPrePage(Integer prePage) {
        this.prePage = prePage;
    }

    public Boolean getHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(Boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public Boolean getHasPrePage() {
        return hasPrePage;
    }

    public void setHasPrePage(Boolean hasPrePage) {
        this.hasPrePage = hasPrePage;
    }

    public List<PageBean.Number> getNumberList() {
        return numberList;
    }

    public void setNumberList(List<PageBean.Number> numberList) {
        this.numberList = numberList;
    }



    /**
     * total.
     *
     * @return  the total
     */
    public Long getTotal() {
        return total;
    }

    /**
     * total.
     *
     * @param   total    the total to set
     */
    public void setTotal(Long total) {
        this.total = total;
    }

    public LayuiPageResult(){

    }

    public LayuiPageResult(Pagination<? extends Object> pagination){
        setParms(pagination);
    }

    public void init(Pagination<? extends Object> pagination){
        setParms(pagination);
    }

    private void setParms(Pagination<? extends Object> pagination){
        PageBean<?> pageBean = new PageBean(pagination.getPageNo()-1,pagination.getTotal(),pagination.getPageSize(),pagination.getList());
        pageBean.setPageNumberSize(DEFAULT_PAGENUMBER_SIZE);
        this.pageNumber=pageBean.getPageNumber()+1;
        this.pageSize=pageBean.getPageSize();
        this.totalPage=pageBean.getPagesCount();
        this.total=pageBean.getTotal();
        this.isFirstPage=pagination.isFirstPage();
        this.isLastPage=pagination.isLastPage();
        this.hasNextPage=pageBean.hasNextPage();
        this.hasPrePage=pageBean.hasPreviousPage();
        List<PageBean.Number> numberList = pageBean.getNumberList();
        for(PageBean.Number number : numberList){
            number.setNumber(number.getNumber()+1);
        }
        this.numberList=numberList;
        this.setPage(pageBean);
        this.setBody(pagination.getList());
    }

    public void setPage(PageBean pageBean){
        if(this.page == null){
            this.page=new LinkedHashMap<String, Object>();
        }
        page.put(TOTAL, pageBean.getTotal());
        page.put(CODE, ErrorCode.suc.getCode());
        page.put(MSG, ErrorCode.suc.getMsg());
        page.put(ROWS, pageBean.getRows());
        page.put(PAGE, this);
    }

    /**
     * 增加扩展信息
     * <p>Title: put</p>
     * @param key
     * @param value
     */
    public void put(String key,Object value){
        if(this.page==null){
            this.page=new LinkedHashMap<String, Object>();
        }
        page.put(key, value);
    }


    @Override
    public String readerJson(){

        return FastJsonUtils.toJSONString(this.page);
    }

    @Override
    public String readerJson(ComplexPropertyPreFilter filter){
        return FastJsonUtils.toJSONString(this.page,filter);
    }
}
